#include "CUi.h"
#include"CUser.h"
#include<iostream>
using namespace std;



bool CUi::_adduser(const char *na, const char *jo, unsigned long &ph, bool &se, unsigned short &age)
{
	Alluser[CUser::number-1] = new CUser(ph, na, jo, se, age);
	return true;
}

void CUi::_deleteuser(short number)
{
	Alluser[number]->~CUser();
	Alluser[number] = NULL;
	swap(Alluser[number], Alluser[CUser::number-1]);
	CUser::number--;

}

void CUi::_checkUser(const char *&na)
{
	for (int a = 0; a < CUser::number; a++)
	{
		if (!strcmp(na, Alluser[a]->User_Name))
		{
			cout << Alluser[a]->Age << Alluser[a]->Job << Alluser[a]->Phonenumber << Alluser[a]->Sex
				<< Alluser[a]->User_Name;
			break;
		}
	}
}

CUi::CUi(int num)
{
	Alluser = new CUser*[num];
	Totaluser = num;
	for (short a = 0; a < num; a++)
	{
		Alluser[a] = NULL;
	}
}

bool CUi::Ui_adduser(bool)
{
	if (Alluser[0]->number < Totaluser) {
		char na[20]; char jo[20]; unsigned long ph;
		bool se; unsigned short age; int number;
		scanf_s("%s", na);
		scanf_s("%s", jo);
		scanf_s("%ul", &ph);
		scanf_s("%b", &se);
		scanf_s("%hd", &age);
		
		return _adduser(na, jo, ph, se, age);
	}
	
	return false;
}

bool CUi::Ui_deleteuser(bool)
{
	char na[20];
	scanf_s("%s", na);
	
	for (short a = 0; a < Totaluser; a++)
	{
		if (!strcmp(na, Alluser[a]->User_Name))
		{
			_deleteuser(a);
			return true;
			break;
		}
		if (Alluser[a] == NULL)return false; break;
	}
	return false;
}

void CUi::PresetUser(const char * filename)
{
	FILE*file;
	fopen_s(&file, filename, "r");
	char na[20]; char jo[20]; unsigned long ph;
	bool se; unsigned short age; int number;
	while (fscanf_s(file, "%d %s %s %ul %b %hd", &number, na, jo, &ph, &se, &age) != EOF) {
		Alluser[number - 1] = new CUser(ph, na, jo, se, age);
	}
}


CUi::~CUi()
{
	delete[]Alluser;
}
